package com.aipsi.cws.supplier.controller;

import com.aipsi.cws.common.result.AjaxResponse;

import com.aipsi.cws.supplier.domain.Supplier;
import com.aipsi.cws.supplier.dvo.CommonVO;
import com.aipsi.cws.supplier.service.ISupplierService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;


import static com.aipsi.cws.supplier.constants.CacheConstants.*;


/**
 * @author zdh
 */
@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping(path = "/v1")
@Validated
public class SupplierController {
    private final ISupplierService iSupplierService;

    //--------------------GET-----------------------------

    @Cacheable(value = SUPPLIER_CACHE_VALUE)
    @GetMapping("/supplier")
    public @ResponseBody
    AjaxResponse getSupplier(@NotEmpty(message = "supplierName不能为空") @RequestParam String supplierName) {
        return AjaxResponse.success(iSupplierService.getOne(supplierName));
    }

    @Cacheable(value = SUPPLIER_CACHE_VALUE)
    @GetMapping("/suppliers")
    public @ResponseBody
    AjaxResponse getSuppliers(@Validated CommonVO commonVO) {
        return AjaxResponse.success(iSupplierService.getSuppliers(commonVO));
    }

    @Cacheable(value = SUPPLIER_CACHE_VALUE)
    @GetMapping("/suppliers_vagued")
    public @ResponseBody
    AjaxResponse getSuppliersVagued(@RequestParam(required = false) String vaguedName,
                                    @Validated CommonVO commonVO) {
        return AjaxResponse.success(iSupplierService.getSuppliersVagued(vaguedName,vaguedName,vaguedName,commonVO));
    }

    //--------------------POST-----------------------------

    @CacheEvict(value = SUPPLIER_CACHE_VALUE,allEntries = true)
    @PostMapping("/suppliers")
    public @ResponseBody
    AjaxResponse postSupplier(@Valid @RequestBody Supplier supplier) {
        return AjaxResponse.success(iSupplierService.save(supplier));
    }


    //--------------------DELETE-----------------------------

    @CacheEvict(value = SUPPLIER_CACHE_VALUE,allEntries = true)
    @DeleteMapping("/suppliers")
    public @ResponseBody
    AjaxResponse deleteSupplier(@NotEmpty(message = "legalPersonVagued不能为空")@RequestParam String supplierName) {
        return AjaxResponse.success(iSupplierService.remove(supplierName));
    }


    //--------------------PUT-----------------------------

    @CacheEvict(value = SUPPLIER_CACHE_VALUE,allEntries = true)
    @PutMapping("/suppliers")
    public @ResponseBody
    AjaxResponse putSupplier(@Valid @RequestBody Supplier supplier) {
        return AjaxResponse.success(iSupplierService.putSupplier(supplier));
    }

}
